package slash.util;

import java.io.File;
import java.util.*;

import org.newdawn.slick.Animation;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.util.Log;

import slash.constants.Globals;
import slash.constants.GlobalResources;

public class ResourceManager {
	
	/**
     * Enum of the resource types the game is using
     */
    public static enum Types {
            IMAGE
    }
	private static boolean loaded = false;
	private static HashMap<String,Image> images = new HashMap<String,Image>();
	//private HashMap<String,String> mapPaths = new HashMap<String,String>();
	
	public ResourceManager(){
		try {
			loadResources();
		} catch (SlickException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * loads resources into hashmap for easier accessability
	 * @throws SlickException 
	 */
	public void loadResources() throws SlickException{
		if(!loaded){
			load(Types.IMAGE, "sprites");
		}
	}
	
	/*private void loadImages() throws SlickException{
		images.put(ResourcePaths.PLAYER_BK1, new Image(getImagePath(ResourcePaths.PLAYER_BK1)));
		images.put(ResourcePaths.PLAYER_BK2, new Image(getImagePath(ResourcePaths.PLAYER_BK2)));
		images.put(ResourcePaths.PLAYER_FR1, new Image(getImagePath(ResourcePaths.PLAYER_FR1)));
		images.put(ResourcePaths.PLAYER_FR2, new Image(getImagePath(ResourcePaths.PLAYER_FR2)));
		images.put(ResourcePaths.PLAYER_LF1, new Image(getImagePath(ResourcePaths.PLAYER_LF1)));
		images.put(ResourcePaths.PLAYER_LF2, new Image(getImagePath(ResourcePaths.PLAYER_LF2)));
		images.put(ResourcePaths.PLAYER_RT1, new Image(getImagePath(ResourcePaths.PLAYER_RT1)));
		images.put(ResourcePaths.PLAYER_RT2, new Image(getImagePath(ResourcePaths.PLAYER_RT2)));
	}*/
	
	/**
     * Loads a specific type of resource within the given path. Works
     * recursively.
     *
     * @param type
     *            - the resource type
     * @param path
     *            - the main path to search in
     * @throws SlickException
     */
    private void load(Types type, String path) throws SlickException {
            File file = new File(path);
            /* loops through all files in directory */
            for (File curFile : file.listFiles()) {
                    if (curFile.getPath().contains("svn")) {
                            continue;
                    }
                    /* if path is directory, load image inside that directory instead */
                    // directory check
                    if (curFile.isDirectory()) {
                            load(type, curFile.getPath());
                    } else {
                            String ref = curFile.getPath();// gets path of image
                            // makes image key by taking file name and cutting of the file extension
                            // ex: image.gif --> image
                            String key = ref.substring(ref.lastIndexOf(System.getProperty("file.separator")) + 1, ref.lastIndexOf('.'));

                            images.put(key, new Image(curFile.getPath()));// stores image into hashmap
                    }
            }
            Log.debug("Loading Images");
    }

	/**
	 * Converts image global from GlobalResources
	 * into key and returns image with same key
	 * Par: 
	 */
	public static Image getImageFromGlobal(String global){
		return images.get(global);
	}
	
	/**
	 * Takes a global image from GlobalResources
	 * @param str
	 * @return
	 */
	private String getImagePath(String str){
		String path;
		
		path = "sprites/" + str.toLowerCase() + ".gif";
		
		return path;
	}
}
